package control.elements;

import control.elements.helpers.Dark;
import control.elements.helpers.Earth;
import control.elements.helpers.ElementType;
import control.elements.helpers.Fire;
import control.elements.helpers.Ice;
import control.elements.helpers.Light;
import control.elements.helpers.Neutral;
import control.elements.helpers.Thunder;
import control.elements.helpers.Water;
import control.elements.helpers.Wind;

public class Avatar implements Perpetuation{

	/**
	 * 
	 */
	public static int MAX_LEVEL = 99;
	
	/**
	 * The level of the caller of this avatar.
	 */
	private int my_level;
		
	/**
	 * The name of the avatar.
	 */
	private String my_name;
	
	/**
	 * The elemental affiliation of this pet.
	 */
	private ElementType element;
	
	/**
	 * All possible pets.
	 */
	private static String[] avatar_names;
		static {
			avatar_names = new String[18];
			avatar_names[0] = "Carbuncle";
			avatar_names[1] = "Fenrir";
			avatar_names[2] = "Ifrit";
			avatar_names[3] = "Leviathan";
			avatar_names[4] = "Titan";
			avatar_names[5] = "Garuda";
			avatar_names[6] = "Shiva";
			avatar_names[7] = "Ramuh";
			avatar_names[8] = "Diabolos";
			avatar_names[9] = "Light Spirit";
            avatar_names[10] = "Dark Spirit";
            avatar_names[11] = "Fire Spirit";
            avatar_names[12] = "Earth Spirit";
            avatar_names[13] = "Water Spirit";
            avatar_names[14] = "Wind Spirit";
            avatar_names[15] = "Ice Spirit";
            avatar_names[16] = "Thunder Spirit";
            avatar_names[17] = "None";
			             
		}
		
	
	/**
	 * Types of elements per avatar.
	 */
	private static ElementType[] avatar_elements;
		static {
			avatar_elements = new ElementType[18];
			avatar_elements[0] = new Light();
			avatar_elements[1] = new Dark();
			avatar_elements[2] = new Fire();
			avatar_elements[3] = new Water();
			avatar_elements[4] = new Earth();
			avatar_elements[5] = new Wind();
			avatar_elements[6] = new Ice();
			avatar_elements[7] = new Thunder();
			avatar_elements[8] = new Dark();
			avatar_elements[9] = new Light();
			avatar_elements[10] = new Dark();
			avatar_elements[11] = new Fire();
			avatar_elements[12] = new Earth();
			avatar_elements[13] = new Water();
			avatar_elements[14] = new Wind();
			avatar_elements[15] = new Ice();
			avatar_elements[16] = new Thunder();
			avatar_elements[17] = new Neutral();
		}
		
	/**
	 * 
	 */
	private static int[] base_elemental_cost;
		static {
			base_elemental_cost = new int[9];
			base_elemental_cost[0] = 0;
			base_elemental_cost[1] = 18;
			base_elemental_cost[2] = 24;
			base_elemental_cost[3] = 56;
			base_elemental_cost[4] = 74;
			base_elemental_cost[5] = 80;
			base_elemental_cost[6] = 89;
			base_elemental_cost[7] = 99;
			base_elemental_cost[8] = 0;
		}
		
	/**
	 * The base cost of avatars.
	 */
	private static int[] base_cost_avatar;
		static {
			base_cost_avatar = new int[16];
			base_cost_avatar[0] = 0;
			base_cost_avatar[1] = 0;
			base_cost_avatar[2] = 1;
			base_cost_avatar[3] = 10;
			base_cost_avatar[4] = 19;
			base_cost_avatar[5] = 28;
			base_cost_avatar[6] = 38;
			base_cost_avatar[7] = 47;
			base_cost_avatar[8] = 56;
			base_cost_avatar[9] = 65;
			base_cost_avatar[10] = 68;
			base_cost_avatar[11] = 71;
			base_cost_avatar[12] = 74;
			base_cost_avatar[13] = 81;
			base_cost_avatar[14] = 91;
			base_cost_avatar[15] = 0;
		}
		
	/**
	 * The base cost of Carby.
	 */
	private static int[] base_cost_carby;
		static {
			base_cost_carby = new int[12];
			base_cost_carby[0] = 1;
			base_cost_carby[1] = 10;
			base_cost_carby[2] = 18;
			base_cost_carby[3] = 27;
			base_cost_carby[4] = 36;
			base_cost_carby[5] = 45;
			base_cost_carby[6] = 54;
			base_cost_carby[7] = 63;
			base_cost_carby[8] = 72;
			base_cost_carby[9] = 81;
			base_cost_carby[10] = 91;
			base_cost_carby[11] = 0;
		}
		
	/**
	 * The base cost of fenrir.
	 */
	private static int[] base_cost_fenrir;
		static {
			base_cost_fenrir = new int[14];
			base_cost_fenrir[0] = 1;
			base_cost_fenrir[1] = 8;
			base_cost_fenrir[2] = 15;
			base_cost_fenrir[3] = 22;
			base_cost_fenrir[4] = 30;
			base_cost_fenrir[5] = 37;
			base_cost_fenrir[6] = 45;
			base_cost_fenrir[7] = 51;
			base_cost_fenrir[8] = 59;
			base_cost_fenrir[9] = 66;
			base_cost_fenrir[10] = 73;
			base_cost_fenrir[11] = 81;
			base_cost_fenrir[12] = 91;
			base_cost_fenrir[13] = 0;
		}
			
	/**
	 * Creates an avatar.
	 * @param the_level
	 * @param the_name
	 */
	public Avatar(final int the_level, final String the_name) {
		boolean level;
		boolean name;
		
		level = setLevel(the_level);
		name = setName(the_name);
		
		if(!name || !level) {
			throw new IllegalArgumentException();
		} else {
			calculatePerp();
			
		}
	}
	
	/**
	 * Returns the type of element this pet is affiliated with.
	 * @return The element of the avatar.
	 */
	public ElementType getElement(){
		return element;
	}
	
	/**
	 * Sets the name of the avatar. Ensures that it is a possible avatar.
	 * @param theName the given name of the avatar to summon.
	 * @return True if the name was a legitimate avatar.
	 */
	private boolean setName(String theName) {
		boolean success = false;
		for(int i = 0; i < avatar_names.length; i++) {
			if (theName.equals(avatar_names[i])){
				my_name = theName;
				element = avatar_elements[i];
				success = true;
				break;
			}
		}
		return success;
	}
	
	/**
	 * 
	 */
	public String getName() {
		return my_name;
	}



	/**
	 * Sets the level of the master of this avatar. 
	 * Constraint: Level must be between Level 1 and 95.
	 * @param theLevel The level of the master
	 * @return Returns true if the level was within the constraints.
	 *  False otherwise.
	 */
	private boolean setLevel(int theLevel) {
		boolean success;
		if(theLevel <= MAX_LEVEL && theLevel > 0) {
			my_level = theLevel;
			success = true;
		} else {
			success = false;
		}
		return success;
	}

	/**
	 * Determines the perpetuation cost of this avatar based on level and name.
	 * @return The amount of perpetuation for this avatar.
	 */
	@Override
	public int calculatePerp() {
		int max = 0;
		int perp_value = 0 ;
		int pos = 0;
		for(int i = 0; i < avatar_names.length; i++) {
			if (my_name.equals(avatar_names[i])) {
				pos = i;
			}
		}
		

		if (pos == 0){
			for(int i = 0; i < base_cost_carby.length; i++) {
				if (my_level >= base_cost_carby[i] && base_cost_carby[i] >= max) {
					max = base_cost_carby[i];
					perp_value = i + 1 ;
				}
			}	

		} else if (pos == 1) {
			
				for(int i = 0; i < base_cost_fenrir.length; i++) {
					if (my_level >= base_cost_fenrir[i] && base_cost_fenrir[i] >= max) {
						max = base_cost_fenrir[i];
						perp_value = i + 1 ;
					}
				}
		}else if (pos == 17) {
			
			perp_value = 0;
			
		}else if (pos >= 9) {
			
			for(int i = 0; i < base_elemental_cost.length; i++) {
				if (my_level >= base_elemental_cost[i] && base_elemental_cost[i] >= max) {
					max = base_elemental_cost[i];
					perp_value = i + 1 ;
				}
			}
		}	
		else {
		
				for(int i = 0; i < base_cost_avatar.length; i++) {
					if (my_level >= base_cost_avatar[i] && base_cost_avatar[i] >= max) {
						max = base_cost_avatar[i];
						perp_value = i + 1 ;
					}		
				}
		}
		return perp_value;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		sb.append(my_name);
		
		return sb.toString();
		
	}

}
